package com.goldsign.pay.common.util;


import com.goldsign.pay.common.util.rsa.RsaUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @author xiaojingjing
 * @Date 2018年5月11日
 * @Description app密钥管理entity
 */
public class AppSignManager implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected static ConcurrentHashMap<String,Map<String,Object>> signMap = new ConcurrentHashMap<String,Map<String,Object>>();

	public static ConcurrentHashMap<String, Map<String, Object>> getSignMap() {
		return signMap;
	}

	public static void setSignMap(String appId,Map<String,Object> keyPair) {
		signMap.put(appId, keyPair);
	}
	
	public AppSignManager(){
		
	}

	public static String getPublicKey(String appId) throws Exception{
		Map<String,Object> keyPair = signMap.get(appId);
		if(keyPair == null){
			keyPair = RsaUtils.genKeyPair();
			setSignMap(appId,keyPair);
		}
		return RsaUtils.getPublicKey(keyPair);
	}
	
	public static String getPrivateKey(String appId) throws Exception{
		Map<String,Object> keyPair = signMap.get(appId);
		if(keyPair == null){
			return "";
		}
		return RsaUtils.getPrivateKey(keyPair);
	}

}
